package com.jysz.certificate.persistence.model.pm;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;

import com.jysz.certificate.persistence.enums.PerformanceAssignStatus;
import com.jysz.certificate.persistence.model.mf.BillOption;
import com.jysz.certificate.persistence.model.system.Shop;
import com.jysz.certificate.persistence.model.system.User;
import com.jysz.webcore.ssh.entity.AbstractBaseEntity;
import com.jysz.webcore.utils.ReflectionUtils;

/**
 * @author <a href="mailto:javaengine@outlook.com">Kaca Rich</a>
 *
 */
@Entity
@Table(name = "pm_performance_assign_shop_detail")
@org.hibernate.annotations.Entity
public class PerformanceAssignShopDetail extends AbstractBaseEntity<Long>  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private PerformanceAssign performanceAssign;
	
	private Shop shop;

//	private PmUserList pmUserList;
	
	private List<User> areaUsers;//当前时间组织架构上的区域经理
	
	private double payinAmount; //管理费实收金额
	private double totalAmount; //所有驻店员绩效总额
	private double areaTotalAmount; //区域经理绩效总额
	
//	/** 费用明细*/
//	private List<BillOption> billOptions;
	
	/** 备注*/
	private String remark;
	
	/** 月份费用明细*/
	private List<PAShopMonthDetail> paShopMonthDetails;
	
	private PerformanceAssignStatus status; //区域经理确认存储状态。
	
	private User confirmUser;
	private Date confirmDate;
	
	@Column(name = "_total_amount")
	public double getTotalAmount() {
		return totalAmount;
	}
	
	@Column(name = "_payin_amount")
	public double getPayinAmount() {
		return payinAmount;
	}
	
	public void setPayinAmount(double payinAmount) {
		this.payinAmount = payinAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

	@ManyToOne(fetch = FetchType.LAZY)  
	@JoinColumn(name = "_performance_assign_id") 
	public PerformanceAssign getPerformanceAssign() {
		return performanceAssign;
	}

	public void setPerformanceAssign(PerformanceAssign performanceAssign) {
		this.performanceAssign = performanceAssign;
	}

	@ManyToOne(fetch = FetchType.LAZY)  
	@JoinColumn(name = "_shop_id") 
	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}


	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "rs_pm_assign_shop_detail_list_sys_user_", joinColumns = @JoinColumn(name = "_area_user_id"), inverseJoinColumns = @JoinColumn(name = "_user_id"))
	public List<User> getAreaUsers() {
		return areaUsers;
	}

	public void setAreaUsers(List<User> areaUsers) {
		this.areaUsers = areaUsers;
	}

//	@ManyToOne(fetch = FetchType.LAZY)  
//	@JoinColumn(name = "_pm_user_list_id") 
//	public PmUserList getPmUserList() {
//		return pmUserList;
//	}
//
//	public void setPmUserList(PmUserList pmUserList) {
//		this.pmUserList = pmUserList;
//	}

	@Column(name = "_area_totalamount")
	public double getAreaTotalAmount() {
		return areaTotalAmount;
	}

	public void setAreaTotalAmount(double areaTotalAmount) {
		this.areaTotalAmount = areaTotalAmount;
	}

	@Column(name = "_remark")
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}


	@OneToMany(targetEntity = PAShopMonthDetail.class, cascade = { CascadeType.ALL }, mappedBy = "performanceAssignShopDetail")
	public List<PAShopMonthDetail> getPaShopMonthDetails() {
		return paShopMonthDetails;
	}

	public void setPaShopMonthDetails(List<PAShopMonthDetail> paShopMonthDetails) {
		this.paShopMonthDetails = paShopMonthDetails;
	}

	@Column(name = "_status")
	@Enumerated(EnumType.ORDINAL)
	public PerformanceAssignStatus getStatus() {
		return status;
	}

	public void setStatus(PerformanceAssignStatus status) {
		this.status = status;
	}

	@ManyToOne(fetch = FetchType.LAZY)  
	@JoinColumn(name = "_confirm_user_id")
	public User getConfirmUser() {
		return confirmUser;
	}

	public void setConfirmUser(User confirmUser) {
		this.confirmUser = confirmUser;
	}

	@Column(name = "_confirm_date")
	public Date getConfirmDate() {
		return confirmDate;
	}

	public void setConfirmDate(Date confirmDate) {
		this.confirmDate = confirmDate;
	}
	
//	@OneToMany(targetEntity = BillOption.class, cascade = { CascadeType.REFRESH}, mappedBy = "performanceAssignShopDetail")
//	public List<BillOption> getBillOptions() {
//		return billOptions;
//	}
//
//	public void setBillOptions(List<BillOption> billOptions) {
//		this.billOptions = billOptions;
//	}

//	@Column(name = "_monthDate", length = 7)
//	public String getMonthDate() {
//		return monthDate;
//	}
//
//	public void setMonthDate(String monthDate) {
//		this.monthDate = monthDate;
//	}
	
	
	@Transient
	public String getAreaUserNames(){
		if(this.getAreaUsers() != null){
			return ReflectionUtils.convertElementPropertyToString(this.getAreaUsers(), "realName", ",");
		}
		return "";
	}
	
}
